/**
 *
 */
package com.dxt.common;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

/**
 * @author Administrator
 *
 *
 *2015-4-28
 */
public class HttpUtil {
	 private static int SocketTimeout = 30000;//3秒
	 private static int ConnectTimeout = 30000;//3秒
	 private static Boolean SetTimeOut = true;

	 /**
	 * 功能描述： http的post 请求，请求内容为json或xml
	 * @param url 请求url
	 * @param type content-type 类型 枚举值为xml/json
	 * @param content 请求内容
	 * @throws ParseException
	 * @throws IOException
	 * @author liuqilu
	 * @since 2015-4-28
	 */
	public static String getHttpPostResponse(String url,String type,String content,String token) throws ParseException, IOException{
		 CloseableHttpClient httpclient = HttpClients.createDefault();
		 String jsonStr =null;
	        try {
	            HttpPost httpPost = new HttpPost(url);
	            if (SetTimeOut) {
		            RequestConfig requestConfig = RequestConfig.custom()
		                    .setSocketTimeout(SocketTimeout)
		                    .setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间
		            httpPost.setConfig(requestConfig);
		        }
	            if(null!=type){
	            	if("xml".equalsIgnoreCase(type)||"json".equalsIgnoreCase(type)){
	            		httpPost.addHeader("content-type", "application/"+type);
	            	}
	            }
				httpPost.addHeader("X-Access-Token",token);
	            httpPost.setEntity(new StringEntity(content, Consts.UTF_8));
	            CloseableHttpResponse response = httpclient.execute(httpPost);
	            jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
	            try {
	            	httpPost.abort();
	            } finally {
	                response.close();
	            }
	        } finally {
	            httpclient.close();
	        }
		return jsonStr;
	}

	/**
	 * 功能描述： http的get 请求
	 * @param url 请求url
	 * @param token 鉴权码
	 * @param content 请求内容
	 * @throws ParseException
	 * @throws IOException
	 * @author liuqilu
	 * @since 2015-4-28
	 */
	public static String getHttpGetResponse(String url,String token,String content) throws ParseException, IOException{
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String jsonStr =null;
		try {
			HttpGet httpGet = new HttpGet(url+JsonUtil.jsonToUrlParams(content));
			if (SetTimeOut) {
				RequestConfig requestConfig = RequestConfig.custom()
						.setSocketTimeout(SocketTimeout)
						.setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间
				httpGet.setConfig(requestConfig);
			}
			httpGet.addHeader("X-Access-Token",token);
			CloseableHttpResponse response = httpclient.execute(httpGet);
			jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
			try {
				httpGet.abort();
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
		return jsonStr;
	}

}
